Imports DAL
Imports MyClasses
Imports System.Configuration

Public Class frmHolidays
    Dim _intHolidayID As Integer

    Private Sub frmHolidays_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Try
            If Not (ConfigurationManager.AppSettings("FormMode") = "R") Then
                If isUpdate() Then
                    Select Case BCommon.ShowMsgError(10081, "YNC")
                        Case Windows.Forms.DialogResult.Yes
                            If ValidateData() Then
                                Call SaveData()
                            Else
                                e.Cancel = True
                            End If
                        Case Windows.Forms.DialogResult.Cancel
                            e.Cancel = True
                        Case Windows.Forms.DialogResult.No
                    End Select
                End If
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "frmHolidays_FormClosing")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub frmHolidays_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        'Rauhl_ITS@20073012 : Add following code display help
        Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Holidays")
        e.Cancel = True
    End Sub

    Private Sub frmHolidays_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If e.KeyCode = Keys.F1 Then
            'Rauhl_ITS@20073012 : Add following code display help
            e.SuppressKeyPress = True
            Help.ShowHelp(Me, "Pharma.chm", HelpNavigator.KeywordIndex, "Holidays")
        End If
    End Sub

    Private Sub frmHolidays_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        dtpHoliday.Value = Now.Date
        Call Display_Data()
    End Sub
   
    Private Sub Display_Data()
        Try
            btnDelete.Enabled = False
            Call BCommon.SetFormStandards(Me)
            Call BCommon.SetDatePickerMaxMinDate(dtpHoliday)
            'dtsHolidays = DEmployee.GetHolidayList()
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "Display_Data")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Cursor = Cursors.WaitCursor
        btnDelete.Enabled = False
        Call ClearData()
        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Function isUpdate() As Boolean
        Try
            If Trim(txtDescription.Text) <> txtDescription.Tag Then
                Return True
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "isUpdate")
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.Cursor = Cursors.WaitCursor
        If ValidateData() = False Then Me.Cursor = Cursors.Default : Exit Sub
        Call SaveData()
        Me.Cursor = Cursors.Default
    End Sub

    Private Function SaveData() As Boolean
        Dim objHolidays As New CHolidays
        Dim blnResult As Boolean
        Try
            objHolidays = SetData()
            If btnSave.Text = "&Save" Then
                blnResult = DEmployee.SaveHolidays(objHolidays, "S")
                If blnResult = True Then BCommon.ShowMsgError(3) : ClearData() Else BCommon.ShowMsgError(4)
            Else
                blnResult = DEmployee.SaveHolidays(objHolidays, "U")
                If blnResult = True Then BCommon.ShowMsgError(5) : ClearData() Else BCommon.ShowMsgError(4)
            End If
            If blnResult Then Call Display_Data()
            Return blnResult
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SaveData")
            Me.Cursor = Cursors.Default
            Return False
        End Try
    End Function

    Private Function SetData() As CHolidays
        Dim objHolidays As New CHolidays
        Try
            With objHolidays
                .HolidayID = _intHolidayID
                .DateOfHoliday = dtpHoliday.Value
                .Description = txtDescription.Text
            End With
            Return objHolidays

        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "SetData")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try
    End Function

    Private Sub ClearData()
        txtDescription.Text = ""
        dtpHoliday.Value = Now.Date
        btnSave.Text = "&Save"
    End Sub
    'Divya 25/10/2007  
    Private Function ValidateData() As Boolean

        Dim dtHoliday As New DataTable
        Dim count As Integer = 0
        Try
            If Trim(txtDescription.Text) = "" Then
                BCommon.ShowMsgError(10214)
                txtDescription.Focus()
                Return False
            End If
            If System.DateTime.Today >= dtpHoliday.Value Then
                BCommon.ShowMsgError(10404)
                dtpHoliday.Focus()
                Return False
            End If
            If btnSave.Text = "&Save" Then
                dtHoliday = DEmployee.GetHolidayList()
                If dtHoliday.Rows.Count > 0 Then
                    For count = 0 To dtHoliday.Rows.Count - 1
                        If dtHoliday.Rows(count).Item("HolidayDate").date = dtpHoliday.Value Then
                            BCommon.ShowMsgError(10215)
                            dtpHoliday.Focus()
                            Return False
                        End If
                    Next
                End If
            End If
            Return True
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ValidateData")
            Me.Cursor = Cursors.Default
            Return Nothing
        End Try
    End Function

    Private Sub ViewHolidays()
        Dim dtHoliday As DataSet
        Try
            dtHoliday = DEmployee.GetHolidays(_intHolidayID)
            If dtHoliday.Tables(0).Rows.Count > 0 Then
                With dtHoliday.Tables(0).Rows(0)
                    dtpHoliday.Text = .Item("HolidayDate")
                    txtDescription.Text = .Item("Description")
                    txtDescription.Tag = .Item("Description")
                End With
            End If
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "ViewHolidays")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub btnSrch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSrch.Click
        Dim form As New frmMasterBrowse
        Try
            Me.Cursor = Cursors.WaitCursor
            form.strSearch = "Holidays"
            'Rahul_ITS@20072612
            '<<START
            form.StartPosition = FormStartPosition.Manual
            form.Height = MDIPharmacy.MdiScaleHeight
            form.Width = MDIPharmacy.MdiScaleWidth
            Dim pt As New Point
            pt.Y = ((MDIPharmacy.MdiScaleHeight - form.Height) / 2) + MDIPharmacy.pnlHeading.Height + MDIPharmacy.MenuStrip.Height
            pt.X = (MDIPharmacy.MdiScaleWidth - form.Width) / 2
            form.Location = pt
            form.ShowInTaskbar = False
            '>>END
            form.ShowDialog()
            _intHolidayID = form.strRecordNo
            Call ClearData()
            If _intHolidayID <> 0 Then
                'Call Display_Holidays()
                Call ViewHolidays()
                btnSave.Text = "&Update"
                If ConfigurationManager.AppSettings("FormMode") = "R" Then
                    btnDelete.Enabled = False
                Else
                    btnDelete.Enabled = True
                End If
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnSrch_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub

    Private Sub dtpHoliday_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles dtpHoliday.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then txtDescription.Focus()
    End Sub

    Private Sub txtDescription_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtDescription.KeyDown
        If e.Alt Then Me.Cursor = Cursors.Default : Exit Sub
        If e.KeyCode = Keys.Enter Then btnSave.Focus()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Try
            Me.Cursor = Cursors.WaitCursor
            If (DEmployee.IsHolidayUsed(_intHolidayID)) = 0 Then
                If (DEmployee.DeleteHoliday(_intHolidayID)) > 0 Then
                    BCommon.ShowMsgError(10373)
                    dtpHoliday.Focus()
                    ClearData()
                    Display_Data()
                    btnSave.Text = "&Save"
                    btnDelete.Enabled = False
                End If
            Else
                BCommon.ShowMsgError(10371)
                btnDelete.Focus()
            End If
            Me.Cursor = Cursors.Default
        Catch ex As Exception
            BCommon.ErrorHandler(ex.Message, Me.ProductName, Me.Name, "btnDelete_Click")
            Me.Cursor = Cursors.Default
        End Try
    End Sub
End Class